const enterpriseVue = new Vue({
  el: '#app',
  data() {
    return {
      rules: {
        Prepayment_Scheme_Name: [{ required: true, message: '请输入储值方案名称', trigger: 'blur' }],
        Starting_Amount: [{ required: true, message: '请输入起步值', trigger: 'blur' }],
        Unit_Amount: [{ required: true, message: '请输入单位值', trigger: 'blur' }],
        Gift_Amount: [{ required: true, message: '请输入赠送值', trigger: 'blur' }],
        Execute_Date: [{ required: true, message: '请选择执行时间', trigger: 'change' }],
      },
      type: '',
      form: {     //储值方案
        Prepayment_Scheme_SerialNumber: '',
        Prepayment_Scheme_Name: '',
        Starting_Amount: '',
        Unit_Amount: '',
        Gift_Amount: '',
        Execute_Date: '',
        Enterprise_Info_SerialNumber: $.cookie("enterprise"),
        Children: {
          prepayment_scheme_storescope: [{     //储值方案适用店铺
            Prepayment_Scheme_StoreScope_SerialNumber: "",
            Prepayment_Scheme_SerialNumber: '',
            Store_SerialNumber: '',
            App_ID: ''
          }]
        }
      },
      store: [],
      loading: false,
    }
  },
  mounted() {
    let Prepayment_Scheme_SerialNumber = sessionStorage.getItem("Prepayment_Scheme_SerialNumber");
    console.log(Prepayment_Scheme_SerialNumber);
    this.getData();
    this.getList(Prepayment_Scheme_SerialNumber);

  },
  methods: {
    getData() {
      let _this = this;
      $.ajax({
        url: DomainName + '/getInquiryAction',
        type: 'POST',
        data: {
          Data: 'store_info',
          token: $.cookie("token")
        },
        success: function (res) {
          _this.store = res
        },
        error: function () {
          _this.$message.error("操作失败，请稍后再试")
        },
        complete: function (XMLHttpRequest) {
          _this.loading = false
          if (XMLHttpRequest.getResponseHeader("REDIRECT") == "REDIRECT") {
            window.parent.document.location.href = "../../../../" + XMLHttpRequest.getResponseHeader("CONTENTPATH");
          }
        }
      })
    },
    getList(Prepayment_Scheme_SerialNumber) {
      const _this = this
      $.ajax({
        type: 'POST',
        url: DomainName + '/getSubtableInquiryOriginal?token=' + $.cookie("token"),
        data: {
          JSONObject: JSON.stringify({
            prepayment_scheme: { prepayment_scheme_storescope: {} },
          }),
          Primary_Key: Prepayment_Scheme_SerialNumber,
        },
        complete: function (XMLHttpRequest) {
          if (XMLHttpRequest.getResponseHeader("REDIRECT") == "REDIRECT") {
            window.parent.document.location.href = "../../../" + XMLHttpRequest.getResponseHeader("CONTENTPATH");
          }
        },
        success: function (Result) {
          if (Result.Status == 'success') {
            console.log(Result);
            _this.form = Result.prepayment_scheme[0]
          }
        },
        dataType: "json"
      });
    },
    submitForm() {
      let _this = this
      _this.loading = true
      const { add, modify } = _this.deal()
      let data = {}
      if (add == null) {
        data = { Data: JSON.stringify({ Modify: { prepayment_scheme: [modify] } }) }
      } else {
        data = {
          Data: JSON.stringify(
            {
              Modify: { prepayment_scheme: [modify] },
              Add: { prepayment_scheme: [add] }
            }
          ),
        }
      }
      $.ajax({
        url: DomainName + '/getEditAction?token=' + $.cookie("token"),
        type: 'POST',
        data,
        success: function (res) {
          _this.loading = false
          console.log('提交结果', res)
          _this.$message.success("提交成功")
          window.history.go(-1);
        },
        error: function () {
          _this.$message.error("操作失败，请稍后再试")
        },
        complete: function (XMLHttpRequest) {
          _this.loading = false
          if (XMLHttpRequest.getResponseHeader("REDIRECT") == "REDIRECT") {
            window.parent.document.location.href = "../../../../" + XMLHttpRequest.getResponseHeader("CONTENTPATH");
          }
        }
      })
    },
    deal() {
      const add = deepClone(this.form)
      const modify = deepClone(this.form)
      let add_prepayment_scheme_storescope = [], modify_prepayment_scheme_storescope = []
      for (const i in modify.Children.prepayment_scheme_storescope) {
        const obj = modify.Children.prepayment_scheme_storescope[i];
        if (!Object.keys(obj).some(item => item.includes('_Original'))) {
          add_prepayment_scheme_storescope.push(obj)
        } else {
          modify_prepayment_scheme_storescope.push(obj)
        }
      }
      add.Children.prepayment_scheme_storescope = add_prepayment_scheme_storescope

      modify.Children.prepayment_scheme_storescope = modify_prepayment_scheme_storescope

      if (add_prepayment_scheme_storescope.length == 0) {
        return { add: null, modify };
      }
      return { add, modify };
    },
    Change_Store_SerialNumber(index) {  //选择店铺获取对应App_ID
      let _this = this
      $.ajax({
        url: DomainName + '/getprepaymentSchemeAppIDAction',
        type: 'POST',
        data: {
          Store_SerialNumber: _this.form.Children.prepayment_scheme_storescope[index].Store_SerialNumber,
          token: $.cookie("token")
        },
        success: function (res) {
          if (res.length == 1) {
            _this.form.Children.prepayment_scheme_storescope[index].App_ID = res[0].App_ID
          } else {
            _this.$message.error("该店铺未绑定App_ID")
            _this.form.Children.prepayment_scheme_storescope[index].App_ID = ''
          }
        },
        error: function () {
          _this.$message.error("操作失败，请稍后再试")
        },
        complete: function (XMLHttpRequest) {
          if (XMLHttpRequest.getResponseHeader("REDIRECT") == "REDIRECT") {
            window.parent.document.location.href = "../../../../" + XMLHttpRequest.getResponseHeader("CONTENTPATH");
          }
        }
      })
    },
    add_prepayment_scheme_storescope() {
      this.form.Children.prepayment_scheme_storescope.push({     //储值方案适用店铺
        Prepayment_Scheme_StoreScope_SerialNumber: "",
        Prepayment_Scheme_SerialNumber: this.form.Prepayment_Scheme_SerialNumber,
        Store_SerialNumber: '',
        App_ID: ''
      })
    },
    remove_prepayment_scheme_storescope() {
      if (this.form.Children.prepayment_scheme_storescope.filter(item => item.Row_Status != '1').length > 1) {
        let i = this.form.Children.prepayment_scheme_storescope.filter(item => item.Row_Status != '1').length - 1
        let obj = this.form.Children.prepayment_scheme_storescope.filter(item => item.Row_Status != '1')[i]
        if (!Object.keys(obj).some(item => item.includes('_Original'))) {
          this.form.Children.prepayment_scheme_storescope.pop()
        } else {
          this.$confirm('警告！此操作将删除一条适用企业, 是否继续?', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning',
          }).then(() => {
            this.form.Children.prepayment_scheme_storescope.filter(item => item.Row_Status != '1')[i].Row_Status = '1'
          }).catch(() => {
            this.$message({
              type: 'warning',
              message: '已取消删除'
            });
          });
        }
      } else {
        this.$message({
          message: '不能在删除了',
          type: 'warning'
        })
      }
    },
  }
})